<?php
namespace Admin\Controller;
use Think\Controller;
class ContestController extends Controller {
    public function ContestList(){

        // $m_id = $_GET['m_id'];

        $contestsModel = M('contests');
        $contests = $contestsModel -> order("contest_id")->select();
         $this->index=$contests;
        // dump($masters);
        $this->display();
    
    }

    public function delcontest($contest_id){
    $contestsModel=M('contests'); 
    //where实在构建条件
    if (false!==$contestsModel->where("contest_id=$contest_id"/*必须是双引号*/)->delete()) {
            $this->success('删除成功！');
        } else {
            $this->error('删除失败');
        }

    }

     public function addContest()
  {
    if(IS_POST)
    {
      
      $data['contest_name'] = I('post.contest_name');
      $data['contest_intro'] = I('post.contest_intro');
      
      $data['contest_count'] = I('post.contest_count');
      $data['contest_phone'] = I('post.contest_phone');
      
      
      if(empty($data['contest_name']))
      {
        $this->error('比赛名称必须填写');
      }
      elseif(empty($data['contest_intro']))
      {
        $this->error('比赛内容必须填写');
      }
      else
      {
        $data['contest_intro'] = md5(md5($data['contest_intro']) );
        
        $contests = M('contests');
        if($_FILES['file']['error']==0 ){
            $info=$this->upload();
            $name=$info["file"]["savename"];
        }
        else{
            $name="default.jpg";
        }
        if($contests->create()){
            $contests->contest_pic1=$name; 
            if(false!==$contests->add())
                      {
                $this->success('添加成功!','ContestList');
            }
            else{$this->error('添加失败');}
        }else{
            $this->error($contests->getError());
        }
      }
    }
    else
    { 
      $this->display();
    }    

  }
    public function upload(){
        $upload = new \Think\Upload();// 实例化上传类
        $upload->maxSize   =     3145728 ;// 设置附件上传大小
        $upload->exts      =     array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
        $upload->rootPath  =     'C:\Users\dell\Desktop\新建文件夹'; // 设置附件上传根目录  //  
        $upload->autoSub = false;//关闭子目录，默认true
        $upload->savePath  =     ''; // 设置附件上传(子)目录，默认目录名为年月日
        $info   =   $upload->upload();  // 上传文件 
        if(!$info) {
            $this->error($upload->getError());    
        }
        else{
            return $info;
        }                    
    }


     public  function editContest($contest_id){
    $contestInfos = M('contests');
    $contests=$contestInfos->where("contest_id=$contest_id")->find();
    $this->contestInfos=$contests;
    $this->display();
  }

  public function  doUpdate($contest_id){
    $contestInfos = M('contests');
    // $data['m_intro']=$_SESSION['m_intro'];
    // $i = $data['m_intro'];
    //接收用户输入的内容
    $data['contest_name'] = trim(I('post.contest_name'));
    $data['contest_intro'] = trim(I('post.contest_intro'));
    $data['contest_count'] = trim(I('post.contest_count'));
    $data['contest_phone'] = trim(I('post.contest_phone'));
    $contestns=$contestInfos->where("contest_id=$contest_id")->save($data);

    if(false !== $contests){
      $this->success('修改成功！','ContestList');
    }else{
      $this->error('修改失败');
    }
  }


}
?>